<template>
  <div class="login-container">
    <div class="login-box">
      <div class="user-img">
        <img src="../assets/ui.jpg" alt>
      </div>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0"
        :model="loginForm"
        class="login-form"
        :rules="loginFormRules"
        ref="loginFormRef"
      >
        <!-- 用户名区域 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码区域 -->
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="login-btns">
          <el-button type="primary" @click="loginIn">登录</el-button>
          <el-button type="info" @click="resetMsg">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginApi } from '../components/api/index.js'
export default {
  data: function() {
    return {
      // 登录的双向绑定数据
      loginForm: {
        username: "",
        password: ""
      },
      // 表单验证的对象
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 6, max: 15, message: "密码输入错误", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    resetMsg() {
      // console.log(this);
      this.$refs.loginFormRef.resetFields();
    },
    loginIn() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          this.$message({
            showClose: true,
            message: "登陆失败",
            type: "error"
          });
        } else {
          var { data: res } = await loginApi(this.loginForm)
          if(res.meta.status !== 200) return
          // 保存token值到sessionStorage
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
          this.$message({
            showClose: true,
            message: "登陆成功",
            type: "success"
          });
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login-container {
  display: flex;
  background-color: #2b4b6b;
  height: 100%;
  justify-content: center;
  align-items: center;
}
.login-box {
  position: relative;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  .user-img {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #eee;
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login-btns {
  display: flex;
  justify-content: flex-end;
}
.login-form {
  box-sizing: border-box;
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
}
</style>
